# -*- coding: utf-8 -*-

import unittest
import os.path

from kivy.lang import Builder

from players_order import PlayersOrderWidget, rgb
from presenter_mock import PresenterMock
from model.gamestate import *


class PlayersOrderTest(unittest.TestCase):
    def setUp(self):
        Builder.load_file(os.path.join(os.path.dirname(__file__),
                                       'playersorder.kv'))
        players = Players([Player('Andrey', []), Player('Boris', []),
                           Player('Charlie', []), Player('Dmitry', [])])
        players.set_next_pair_strategy(NextPairStrategy(players.players))
        hat = Hat(['стул', 'шапка', 'кастрюлька'])
        game_round = GameRound(20, [], '', (players.players[0],
                                            players.players[2]))
        self.gamestate = GameState(players, hat,
                                   game_round,
                                   0, [], None, Settings(20, 30))
        self.presenter = PresenterMock(Phase.FROM_TO)

    def test_buttons_press(self):
        widget = PlayersOrderWidget(self.gamestate, self.presenter)
        self.assertEquals(self.presenter.is_phase_changed, False)
        widget.shuffle()
        self.assertEquals(self.presenter.is_phase_changed, False)
        widget.finish()
        self.assertEquals(self.presenter.is_phase_changed, True)

    def test_many_users(self):
        players = []
        for i in range(100):
            players.append(Player(str(i), []))
        self.gamestate.players = Players(players)
        widget = PlayersOrderWidget(self.gamestate, self.presenter)
        widget.shuffle()
        self.assertEquals(self.presenter.is_phase_changed, False)
        widget.finish()
        self.assertEquals(self.presenter.is_phase_changed, True)

    def test_rgb(self):
        self.assertEquals(rgb('000000'), [0, 0, 0, 1])
        self.assertEquals(rgb('FFFFFF'), [1, 1, 1, 1])
        self.assertEquals(rgb('FF0000'), [1, 0, 0, 1])
        self.assertEquals(rgb('00FF00'), [0, 1, 0, 1])
        self.assertEquals(rgb('0000FF'), [0, 0, 1, 1])


if __name__ == '__main__':
    unittest.main()
